<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{

    public function index()
    {
        // 如果是跳转来的页面就跳转
        if (isset($_GET['redirect'])) {
            $this->assign('redirect', $_GET['redirect']);
        } else {
            $this->assign('redirect', "");
        }
        if (isset($_COOKIE['user_id'])) {
            $this->redirect("email/every_day_issue");
        }
        $this->display();
    }

    public function authenticate()
    {
        $_POST['username'] = isset($_POST['username']) ? trim($_POST['username']) : '';
        $_POST['password'] = isset($_POST['password']) ? trim($_POST['password']) : '';

        /* 检查密码是否正确 */
        $sql = "SELECT * FROM zt_user WHERE deleted = '0' AND account = '" . $_POST['username'] . "' AND password = '" . md5($_POST['password']) . "'";

        $rowRes = M()->query($sql);

        if ($rowRes) {
            $row = $rowRes[0];
            $sql = "SELECT
                    *
                FROM
                    zt_dept
                WHERE
                    id = " . $row['dept'];
            $deptRes = M()->query($sql);
            // 登录成功
            $this->_setAdminSession($row['id'], $row['realname'], $row['dept'], $deptRes[0]['name'], $row['role']);
            $this->_setAdminCookie($row['id'], $row['realname'], $row['dept'], $deptRes[0]['name'], $row['role']);

	        // 如果是跳转来的页面就跳转
	        if (isset($_GET['redirect'])) {
		        $this->redirect(urldecode($_GET['redirect']));
	        }
            $this->redirect('Email/every_day_issue');
        } else {
            $this->error('您输入的帐号信息不正确。');
        }
    }

    /**
     * 设置管理员的session内容
     *
     * @access  public
     * @param   integer $user_id 管理员编号
     * @param   string $username 管理员姓名
     * @param   string $action_list 权限列表
     * @param   string $last_time 最后登录时间
     * @return  void
     */
    private function _setAdminSession($user_id, $username, $dept, $deptName, $role)
    {
        $_SESSION['user_id'] = $user_id;
        $_SESSION['username'] = $username;
        $_SESSION['dept'] = $dept;
        $_SESSION['dept_name'] = $deptName;
        $_SESSION['role'] = $role;
    }

    private function _setAdminCookie($user_id, $username, $dept, $deptName, $role)
    {
        $expireTime = 24 * 3600; // 一天
        cookie('user_id', $user_id, $expireTime);
        cookie('username', $username, $expireTime);
        cookie('dept', $dept, $expireTime);
        cookie('dept_name', $deptName, $expireTime);
        cookie('role', $role, $expireTime);
    }

    private function _destroyCookie()
    {
        cookie('user_id', null);
        cookie('username', null);
        cookie('dept', null);
        cookie('dept_name', null);
        cookie('role', null);
    }

    /* 退出登录 */
    public function logout()
    {
        if (isset($_SESSION['user_id'])) {
            session_destroy();
            $this->_destroyCookie();
            $this->success('退出成功！', U('Index/index'));
        } else {
            $this->redirect('Index/index');
        }
    }
}
